package Stackdemo;

import java.util.Stack;

public class MyQueue_Stack {
    private Stack<Integer> s1;
    private Stack<Integer> s2;
    public MyQueue_Stack() {
        this.s1 = new Stack<>();
        this.s2 = new Stack<>();
    }

    //入队
    public void push(int x) {
        s1.push(x);
    }
    //出队
    public int pop() {
        if(empty()){
            return -1;
        }
        if(s2.isEmpty()){
            int size = s1.size();
            for(int i = 0;i<size;i++){
                int x = s1.pop();
                s2.push(x);
            }
            return s2.pop();
        }else{
            return s2.pop();
        }
    }
    //获取队列开头元素
    public int peek() {
        if(empty()){
            return -1;
        }
        if(s2.isEmpty()){
            int find = -1;
            int size = s1.size();
            for(int i = 0;i<size;i++){
                int x = s1.pop();
                find = s2.push(x);
            }
            return find;
        }else{
            return s2.peek();
        }
    }

    public boolean empty() {
        if(s1.isEmpty()&&s2.isEmpty()){
            return true;
        }
        return false;
    }
}
